
<template>
    <div class="skin-label" v-loading="loading">
        <div class="input-box">
            <el-button type="primary" class="btn" @click="toDetail('')">新增</el-button>
        </div>
        <div class="table-box">
            <el-table :data="tableData" ref="table" border style="width: 100%">
                <el-table-column min-width="150" prop="labelName" label="标签名"></el-table-column>
                <el-table-column min-width="150" label="样式">
                    <template slot-scope="scope">
                        <img class="tab-img" v-if="scope.row.icon" :src="scope.row.icon" alt="">
                        <span v-else>无需样式</span>
                    </template>
                </el-table-column>
                <el-table-column width="150" label="状态">
                    <template slot-scope="scope">
                        <p v-if="scope.row.status === 0">隐藏</p>
                        <p v-if="scope.row.status === 1">展示</p>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="管理" prop="time">
                    <template slot-scope="scope">
                        <el-button @click="toDetail(scope.row)" style="margin-left: 10px;" type="text" size="small">编辑</el-button>
                        <el-button v-if="scope.row.status === 0" @click="changeStatus(scope.row, 1)" style="margin-left: 10px;color:#67C23A;" type="text" size="small">上架</el-button>
                        <el-button v-if="scope.row.status === 1" @click="changeStatus(scope.row, 0)" style="margin-left: 10px;color:#F56C6C;" type="text" size="small">下架</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <el-dialog class="skin-label-dialog" :title="dialogTitle" :visible.sync="dialogVisible" width="350px">
            <div class="dialog-con">
                <span><i>*</i> 标签名:</span>
                <el-input type="text" style="width:180px;" v-model.trim="dialogData.labelName"></el-input>
            </div>
            <div class="dialog-con">
                <span><i>*</i> 上传样式:</span>
                <uploadOwnImg v-model="dialogData.icon" overflow="hidden" height="82px"/>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveLabelTrigger">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

export default {
    data () {
        return {
            dialogTitle: '',
            tableData: [],
            dialogData: {},
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false,
            dialogVisible: false
        }
    },
    created () {
        this.getList()
    },
    methods: {
        // 获取列表
        async getList () {
            if (this.loading) return
            this.loading = true

            let res = await this.$http.get('/trade/selectTradeWZRYLabel')
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },
        toDetail (item) {
            console.log(item)
            this.dialogVisible = true
            if (item) {
                this.dialogTitle = '编辑标签'
                this.dialogData = Object.assign({}, item)
            } else {
                this.dialogTitle = '新建标签'
            }
        },
        saveLabelTrigger () {
            if (this.dialogTitle === '新建标签') {
                this.dialogData.status = 0
            }
            this.dialogData.createTime = null
            this.dialogData.isTrusted = null // 不知道哪里来的isTrusted.. 先置空
            this.$http.get('/trade/createTradeWZRYLabel', { params: this.dialogData }).then(res => {
                if (res.code === '0') {
                    this.getList()
                    this.dialogVisible = false
                    this.$message({
                        type: 'success',
                        message: '保存成功'
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: '系统异常，请稍后再试'
                    })
                }
            })
        },
        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList()
        },
        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList()
        },
        changeStatus (item, status) {
            let params = Object.assign({}, item)
            params.status = status
            params.createTime = null
            this.$http.get('/trade/createTradeWZRYLabel', {
                params
            }).then(res => {
                if (res.code === '0') {
                    let message = status < 1 ? '下架成功' : '上架成功'
                    item.status = status
                    this.$message({
                        type: 'success',
                        message
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: '系统异常，请稍后再试'
                    })
                }
            })
        }
    }
}
</script>

<style lang="scss" scoped>
    .skin-label {
        .input-box {
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
        }

        .table-box {
            margin: 20px 0;
            .tab-img {
                width: 126px;
                height: 48px;
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
        .skin-label-dialog {
            /deep/.el-dialog__body {
                padding-top: 0;
            }

            .dialog-con {
                display: flex;
                align-items: center;
                margin-top: 20px;
                span {
                    display: inline-block;
                    width: 100px;
                    i {
                        color: #f56c6c;
                        font-style: normal;
                    }
                }
            }
        }
    }
</style>
